class Solution:
    def add(self, a, b):
        # 找到小数点位置
        def finddot(x):
            for i in range(len(x)):
                if x[i] == '.':
                    return i
            return len(x)
        # 同位数相加
        def add2(x, y):
            r2 = ''
            nextStat = 0
            for i in range(len(x)-1, -1, -1):
                if x[i] == '.':
                    r2 = "." + r2
                    continue
                c = int(x[i])+ int(y[i]) + nextStat
                nextStat = c//10
                r2 = str(c%10)+r2
            return (r2, nextStat)
        # 取补码
        def negativeCode(x):
            r2 = ''
            r3 = ''
            for i in x:
                if i == '.':
                    r2 += '.'
                    r3 += '.'
                else:
                    r2 += str(9 - int(i))
                    r3 += '0'
            r3 = r3[:-1] + '1'
            r2 , _ = add2(r2, r3)
            return r2
        # 删除前后的0
        def deleteZero(x):
            # 找到小数点位置
            xdot = finddot(x)
            for i in range(len(x) - 1, xdot, -1):
                if x[i] != '0':
                    # 截取
                    x = x[:i + 1]
                    break
            # 最后一位是否是0，不是则截取到小数点位置，
            if x[-1] == '0':
                x = x[:xdot + 1]

            for i in range(xdot):
                if x[i] != '0':
                    x = x[i:]
                    break
            if x[0] == '0':
                x = x[xdot - 1:]

            if x[-1] == '.':
                x = x[:-1]
            return x
        # 开始代码
        # 有一个为0时返回另一个
        if a=='0':
            if b=='0':
                return '0'
            else:
                return b
        elif b=='0':
            return a
        # 取符号
        af = 1
        bf = 1
        if a[0] == '-':
            a = a[1:]
            af = -1
        if b[0] == '-':
            b = b[1:]
            bf = -1
        # 取小数点
        adot = finddot(a)
        bdot = finddot(b)
        if len(a) == adot:
            a = a + '.0'
        if len(b) == bdot:
            b = b + '.0'
        # 前后补全0
        if len(a) - adot > len(b) - bdot:
            b = b + '0'*((len(a) - adot) - (len(b) - bdot))
        elif len(a) - adot < len(b) - bdot:
            a = a + '0' * ((len(b) - bdot) - (len(a) - adot))
        if adot>bdot:
            b = '0' * (adot-bdot) + b
        elif adot<bdot:
            a = '0' * (bdot - adot) + a

        # print(a)
        # print(b)
        # 记录当前小数点位置
        nowdot = finddot(a)

        # 记录当符号不同时是否溢出
        newF = 1
        if af * bf < 0:
            # 小的数用补码替换， 大数的符号为新的符号
            if a>b:
                newF = af
                b = negativeCode(b)
            else:
                newF = bf
                a = negativeCode(a)

        # 进行按位相加
        r,nextStat = add2(a, b)
        if nextStat!=0:
            r = str(nextStat) + r

        if af * bf < 0:
            # 根本小数点是否变化来判断是否溢出
            newdot = finddot(r)
            if newdot > nowdot:
                r = r[1:]


        # print(r)
        # 删除前后的0
        r = deleteZero(r)

        if af * bf < 0:
            if newF<0:
                r = '-' + r
        else:
            if af == -1:
                r = '-' + r

        return r




if __name__ == '__main__':
    so = Solution()
    a = '-999.21'
    b = ''
    r = so.add(a,b)
    print(r)
    print((float(a) + float(b)))
